#include <iostream>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
using namespace std;


int main(){
    freopen("in.txt","r",stdin);
    int n,q,x,y;
    cin >> n>>q;
    int a[1000+5];
    for(int i=1;i<=n;i++){
        cin >> a[i];
    }
    sort(a,a+n);
    while(q--){
        cin >> x>>y;
        for(int i=1;i<=n;i++){
            if(a[i]%int(pow(10,x))==y){
                cout << a[i]<<endl;
                break;
            }else if(i==n){
                cout << -1<<endl;
            }
        }
        
    }
    return 0;
}